package com.zimmur.app.mocolink.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zimmur.app.mocolink.dao.AppUserMapper;
import com.zimmur.app.mocolink.dao.UserGradeMapper;
import com.zimmur.app.mocolink.domain.common.Constant;
import com.zimmur.app.mocolink.domain.model.AppUserExample;
import com.zimmur.app.mocolink.domain.model.UserGrade;
import com.zimmur.app.mocolink.domain.model.UserGradeExample;
import com.zimmur.app.mocolink.service.IUserGradeService;

@Service
public class UserGradeServiceImpl implements IUserGradeService {

	@Autowired
	private UserGradeMapper userGradeMapper;
	@Autowired
	private AppUserMapper appUserMapper;
	@Override
	public UserGrade getDefaultGrade() {
		UserGrade userGrade =userGradeMapper.selectDefaultGrade();
		if(userGrade==null){
			userGrade=new UserGrade();
			userGrade.setId(0);
		}
		return userGrade;
	}
	@Override
	public List<UserGrade> selectAll() {
		UserGradeExample example = new UserGradeExample();
		example.setOrderByClause("id asc");
		return userGradeMapper.selectByExample(example);
	}
	@Override
	public UserGrade selectById(Integer gradeId) {
		return userGradeMapper.selectByPrimaryKey(gradeId);
	}
	@Override
	public int add(UserGrade userGrade) {
		if(Constant.byte_1==userGrade.getIsDefault()){
			userGradeMapper.cancelDefault();
		}
		return userGradeMapper.insertSelective(userGrade);
	}
	@Override
	public int update(UserGrade userGrade) {
		if(Constant.byte_1==userGrade.getIsDefault()){
			userGradeMapper.cancelDefault();
		}
		return userGradeMapper.updateByPrimaryKeySelective(userGrade);
	}
	@Override
	public int delete(Integer gradeId) {
		AppUserExample example = new AppUserExample();
		example.createCriteria().andGradeIdEqualTo(gradeId);
		int count = appUserMapper.countByExample(example);
		if(count>0){
			return -1;
		}
		return userGradeMapper.deleteByPrimaryKey(gradeId);
	}

}
